<?php
namespace app\api\controller;

use app\api\controller\Authbase;
use app\api\model\AlbumImgModel;
use app\api\model\AlbumModel;
use think\Controller;

/**
 * 相册接口
 */
class Album extends Authbase
{
    /**
     * 相册首页接口
     * 每页5个
     */
    public function index() {
        $page = input('page', 1);
        $app_id = input('app_id', '');
        $data = AlbumModel::publishAlbums($page, $app_id);
        if (empty($data)) {
            return json(['code' => 0, 'msg' => 'not data']);
        }
        return json(['code' => 1, 'msg' => 'success', 'data' => $data]);
    }

    /**
     * 相册详情接口
     *
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function view() {
        $id = input('id', '');
        $app_id = input('app_id/d', '');
        if (empty($id)) {
            return json(['code' => 0, 'error' => '请传递参数id']);
        }
        $albumM = new AlbumModel();
        $album = $albumM->getAlbumOne($id, $app_id);
        return json(['code' => 1, 'msg' => 'success', 'data' => $album]);
    }

    /**
     * 创建相册
     */
    public function create() {
        $app_id = input('app_id/d', '');
        if (empty($app_id)) {
            return json(['code' => 0, 'error' => '请传递参数app_id']);
        }

        $albumM = new AlbumModel();
        $data = [
            'nick_name' => $this->Auth_Wxuser_name,
            'applet_id' => $app_id,
            'avatar_url' => $this->Auth_Wxuser_avatar,
            'open_id' => $this->Auth_Wxuser_OpenId
        ];
        if(!$res = $albumM->ini($data)) {
            return json(['code' => 0, 'error' => AlbumModel::$error_msg]);
        }

        return json(['code' => 1, 'msg' => 'success', 'album_id' => $res]);
    }

    /**
     * 销毁相册
     */
    public function delete() {
        $album_id = input('album_id/d', '');
        if (empty($album_id)) {
            return json(['code' => 0, 'error' => '请传递参数album_id']);
        }
        if (!AlbumModel::del($album_id)) {
            return json(['code' => 0, 'error' => AlbumModel::$error_msg]);
        }
        return json(['code' => 1, 'msg' => 'success']);
    }

    /**
     * 编辑相册文字接口
     *
     * @return \think\response\Json
     */
    public function text() {
        $post = $this->request->post();
        //var_dump($post);die;
        $album_id = isset($post['album_id'])?$post['album_id']:'';
        $pictures = isset($post['pictures'])?$post['pictures']:'';
        $title = isset($post['title'])?$post['title']:'';
        if (empty($album_id) || empty($pictures) || empty($title)) {
            return json(['code' => 0, 'error' => '请传递参数album_id以及pictures']);
        }
        if (!is_array($pictures)) {
            return json(['code' => 0, 'error' => '参数image_url格式不对']);
        }
        $res = AlbumImgModel::editText($album_id, $title, $pictures);
        if (!$res) {
            return json(['code' => 0, 'error' => AlbumImgModel::$error_msg]);
        }
        return json(['code' => 1, 'msg' => 'success']);
    }

    /**
     * 点击
     */
    public function click() {
        $album_id = input('album_id/d', '');
        if (empty($album_id)) {
            return json(['code' => 0, 'error' => '请传递参数album_id']);
        }
        $model = new AlbumModel();
        $res = $model->where(['id' => $album_id])->setInc('click_num');
        if (!$res) {
            return json(['code' => 0, 'error' => '更新失败']);
        }
        return json(['code' => 1, 'msg' => 'success']);
    }

    /**
     * 分享
     */
    public function share() {
        $album_id = input('album_id/d', '');
        if (empty($album_id)) {
            return json(['code' => 0, 'error' => '请传递参数album_id']);
        }

        $model = new AlbumModel();
        $res = $model->where(['id' => $album_id])->setInc('share_num');
        if (!$res) {
            return json(['code' => 0, 'error' => '更新失败']);
        }
        return json(['code' => 1, 'msg' => 'success']);
    }
}